package com.example.education.service.impl;

import com.example.education.exception.ParamException;
import com.example.education.dao.CourseMapper;
import com.example.education.model.Course;
import com.example.education.service.CourseService;
import com.example.education.utils.FtpUtils;
import org.apache.commons.net.ftp.FTPFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Random;

@Service
@EnableTransactionManagement
public class CourseServiceImpl implements CourseService {

    @Autowired
    CourseMapper courseMapper;

    @Override
    public boolean insertCourse(MultipartFile file,Course course) throws ParamException, IOException {
        if(course==null){
            throw new ParamException("course为空");
        }else if(course.getCateId()==null){
            throw new ParamException("CateId为空");
        }else if(course.getCourseName()==null){
            throw new ParamException("CourseName为空");
        }
        course.setCourseNum(0);
        Integer result =  courseMapper.insertCourse(course);
        if(result>0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    @Transactional
    public boolean insertCourseVideo(MultipartFile file,Integer courseId) throws ParamException{
        if(file==null){
            throw new ParamException("file为空");
        }else if(courseId==null){
            throw new ParamException("courseName为空");
        }
        Course course = courseMapper.getCourseById(courseId);
        int courseNum = course.getCourseNum()+1;
        course.setCourseNum(courseNum);
        courseMapper.updateCourse(course);
        FtpUtils ftpUtils = FtpUtils.getInstance();
        String fileName = courseNum+".flv";
        String folder = course.getVideoAddress();
        ftpUtils.uploadSingleAttachment(file,fileName,folder);
        return true;
    }

    @Override
    public boolean deleteCourseVideo(Integer courseId) throws ParamException{
        if(courseId==null){
            throw new ParamException("courseId为空");
        }
        Course course = courseMapper.getCourseById(courseId);
        String videoAddress = course.getVideoAddress();
        FtpUtils ftpUtils = FtpUtils.getInstance();
        FTPFile[] ftpFiles = ftpUtils.getFileList(videoAddress);
        for (FTPFile ftpFile : ftpFiles) {
            ftpUtils.deleteFile(videoAddress+"/"+ftpFile.getName());
        }
        ftpUtils.deleteDirectory(videoAddress);
        return true;
    }

    @Override
    @Transactional
    public boolean deleteCourse(Integer courseId) throws ParamException {
        if(courseId==null){
            throw new ParamException("参数为空");
        }
        boolean result2 = deleteCourseVideo(courseId);
        Integer result =  courseMapper.deleteCourse(courseId);
        if(result>0 && result2){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean updateCourse(Course course) throws ParamException {
        if(course==null){
            throw new ParamException("参数为空");
        }
        Integer result =  courseMapper.updateCourse(course);
        if(result>0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public Course getCourseById(Integer id) throws ParamException {
        if(id==null){
            throw new ParamException("参数为空");
        }
        return courseMapper.getCourseById(id);
    }

    @Override
    public List<Course> getCourseByIds(List<Integer> ids) throws ParamException {
        if(ids==null){
            throw new ParamException("参数为空");
        }
        return courseMapper.getCourseByIds(ids);
    }

    @Override
    public List<Course> getCourseByCateId(Integer cateId,Integer start,Integer size) throws ParamException {
        if(start==null){
            throw new ParamException("start为空");
        }else if(size==null){
            throw new ParamException("size为空");
        }
        return courseMapper.getCourseByCateId(cateId,start,size);
    }

    @Override
    public List<Course> getCourseByWord(String word,Integer start,Integer size) throws ParamException {
        if(word==null){
            throw new ParamException("word为空");
        }else if(start==null){
            throw new ParamException("start为空");
        }else if(size==null){
            throw new ParamException("size为空");
        }
        return courseMapper.getCourseByWord(word,start,size);
    }

    @Override
    public List<Course> selectAll (Integer page,Integer limit) throws ParamException {
        if(page==null){
            throw new ParamException("page为空");
        }else if(limit==null){
            throw new ParamException("limit为空");
        }
        return courseMapper.selectAll((page-1)*limit,limit);
    }

    @Override
    public Integer getAllCount(){
        return courseMapper.getAllCount();
    }
}
